package org.sy.lab.Java7并发编程实战.一线程管理.二线程信息的获取和设置;

import java.io.IOException;
import java.lang.Thread.State;

public class Main {
	public static void main(String[] args) throws IOException {
		Thread threads[] = new Thread[10];
		Thread.State status[] = new Thread.State[10];
		for(int i=0; i<10; i++) {
			threads[i] = new Thread(new Calculator(i));
			if((i%2)==0) {
				threads[i].setPriority(Thread.MAX_PRIORITY);
			} else {
				threads[i].setPriority(Thread.MIN_PRIORITY);
			}
			threads[i].setName("Thread " + i);
		}
		for(int i=0; i<10; i++) {
			System.out.println("Main: Status of Thread " + i + ": " + threads[i].getState());
			status[i] = threads[i].getState();
		}
		for(int i=0; i<10; i++) {
			threads[i].start();
		}
		boolean finish = false;
		while(!finish) {
			for(int i=0; i<10; i++) {
				if(threads[i].getState() != status[i]) {
					writeThreadInfo(threads[i], status[i]);
					status[i] = threads[i].getState();
				}
			}
		}
	}
	private static void writeThreadInfo(Thread thread, State state) {
		System.out.printf("Main: Id %d - %s\n", thread.getId(), thread.getName());
		System.out.printf("Main: Priority: %d\n", thread.getPriority());
		System.out.printf("Main: Old State: %s\n", state);
		System.out.printf("Main: New State: %s\n", thread.getState());
		System.out.printf("Main: ************************\n");
	}
}
